package com.lsiyun.sync_file.old;

import cn.hutool.crypto.SecureUtil;
import com.lsiyun.sync_file.protocol.SyncFile;
import com.lsiyun.sync_file.protocol.SyncMessage;
import com.lsiyun.sync_file.protocol.SyncProtocol;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Objects;

public class Server {

    private static String syncDirPath = "F:/B";


    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ServerSocket serverSocket = new ServerSocket(8888);
        while (true) {
            Socket socket = serverSocket.accept();

            dealSocketWithNewThread(socket);
        }
    }

    private static void dealSocketWithNewThread(Socket socket) {
        new Thread(new Runnable() {

            File dir = new File(syncDirPath);

            @Override
            public void run() {
                try {
                    int protocol = socket.getInputStream().read();
                    if (protocol != SyncProtocol.C2S_SEND_FILES_INFO) {
                        return;
                    }

                    SyncFile syncDir = (SyncFile) new ObjectInputStream(socket.getInputStream()).readObject();

                    sync(socket, dir, syncDir);

                    socket.close();
                } catch (Exception e) {
                    System.err.println(e);
                }
            }
        }).start();

    }

    private static void sync(Socket socket, File file, SyncFile syncFile) throws IOException {
        if (file.isFile() && syncFile.isFile()) {
            if (file.getName().equals(syncFile.getFileName())) {
                // 同名
                if (Objects.equals(SecureUtil.md5(file), syncFile.getMd5())) {
                    // 同文件
                    return;
                }

                // 判断新旧
                if (file.lastModified() > syncFile.getUpdateTime()) {
                    ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file));
                    ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                    SyncMessage delMsg = new SyncMessage().setType(SyncProtocol.C2S_DELETE_FILE)
                            .setSyncFile(syncFile);
                    outputStream.writeObject(delMsg);
                    outputStream.writeObject(new SyncMessage().setType(SyncProtocol.C2S_ADD_FILE)
                            .setSyncFile(syncFile));

                    byte[] bs = new byte[1024 * 1024 * 10];
                    int s = -1;

                    while ((s = inputStream.read(bs)) > 0) {
                        outputStream.write(bs, 0, s);
                    }

                    inputStream.close();
                } else {
                    ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
                    ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                    FileOutputStream fileOutputStream = new FileOutputStream(file);
                    file.delete();

                    outputStream.writeObject(new SyncMessage().setType(SyncProtocol.C2S_GET_FILE)
                            .setSyncFile(syncFile));

                    byte[] bs = new byte[1024 * 1024 * 10];
                    int s = -1;

                    while ((s = inputStream.read(bs)) > 0) {
                        fileOutputStream.write(bs, 0, s);
                    }

                    fileOutputStream.close();
                }
            }
        }

        // if (file.isDirectory() && syncFile.isDir()) {
        //     for (File file : dir.listFiles()) {
        //
        //     }
        // }
    }

}
